package com.px.action;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.ui.Messages;
import com.px.state.Px2VhState;

import javax.swing.*;
import java.math.BigDecimal;

public class SetHeightAction extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {
        JFrame frame = new JFrame("px to vh");
        JLabel label = new JLabel("设置高度");
        JTextField textField = new JTextField(20);
        Px2VhState state = ServiceManager.getService(Px2VhState.class);
        textField.setText(state.getHeight());
        JButton button = new JButton("提交");
        button.addActionListener(actionEvent -> {
            String height = textField.getText();
            try {
                new BigDecimal(height);
                state.setHeight(height);
                frame.dispose();
            } catch ( NumberFormatException e1 ){
                Messages.showMessageDialog(frame, "unsupport format", "Error", Messages.getInformationIcon());
            }
            catch (Exception exception) {

            }

        });
        JPanel panel = new JPanel();
        panel.add(label);
        panel.add(textField);
        panel.add(button);
        frame.setContentPane(panel);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);


//        Px2VhState state = ServiceManager.getService(Px2VhState.class);
//        JFrame frame = new JFrame("Input Text");
//        JTextField textField = new JTextField(20);
//        textField.setText(state.getLastInput());
//        JButton button = new JButton("Submit");
//        button.addActionListener(actionEvent -> {
//            String inputText = textField.getText();
//            state.setLastInput(inputText);
//            Messages.showMessageDialog(inputText, "Information", Messages.getInformationIcon());
//            frame.dispose();
//        });
//        JPanel panel = new JPanel();
//        panel.add(textField);
//        panel.add(button);
//        frame.setContentPane(panel);
//        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//        frame.pack();
//        frame.setVisible(true);
    }
}
